﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Controls;
using iTeachSoft.CoreObjects;
using iTeachSoft.Presentation.Code.Managers;
using iTeachSoft.Code.Managers;
using iTeachSoft.Presentation.Code.Utility;

namespace iTeachSoft.Presentation.Code.Wrappers
{
    public class SlideThumbnail : Thumb
    {
        #region "Dependency properties"

        public BitmapImage ImageSource
        {
            get { return (BitmapImage)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(SlideThumbnail), null);

        public string SlideText
        {
            get { return (string)GetValue(SlideTextProperty); }
            set { SetValue(SlideTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SlideText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SlideTextProperty =
            DependencyProperty.Register("SlideText", typeof(string), typeof(SlideThumbnail), new UIPropertyMetadata("Unavailable"));


        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(SlideThumbnail), new UIPropertyMetadata(true));


        #endregion

        private Slide mCoreSlide = null;

        public Slide CoreSlide
        {
            get { return mCoreSlide; }
            set
            {
                mCoreSlide = value;
                ReflectSlide();
            }
        }

        public SlideThumbnail()
        {
            BuildContextMenu();
            this.Style = (Style)Application.Current.FindResource("SlideThumbnailStyle");
            
            this.MouseUp += new System.Windows.Input.MouseButtonEventHandler(SlideThumbnail_MouseUp);
            this.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(SlideThumbnail_MouseDoubleClick);

            EventsManagement.Instance.ReflectSlideViewerRequested += new EventHandler(Instance_ReflectSlideViewerRequested);
        }

        private void BuildContextMenu()
        {
            ContextMenu menu = new ContextMenu();

            var miMoveUp = new MenuItem() { Header = "Move up" };
            var miMoveDown = new MenuItem() { Header = "Move down" };
            var miDeleteSlide = new MenuItem() { Header = "Delete slide" };

            miMoveUp.Click += new RoutedEventHandler(miMoveUp_Click);
            miMoveDown.Click += new RoutedEventHandler(miMoveDown_Click);
            miDeleteSlide.Click += new RoutedEventHandler(miDeleteSlide_Click);

            menu.Items.Add(miMoveUp);
            menu.Items.Add(miMoveDown);
            menu.Items.Add(miDeleteSlide);

            this.ContextMenu = menu;
        }

        void miMoveUp_Click(object sender, RoutedEventArgs e)
        {
            int currentIndex = RuntimeStorage.CurrentProject.Slides.IndexOf(mCoreSlide);
            if (currentIndex - 1 >= 0)
            {
                Slide swap = RuntimeStorage.CurrentProject.Slides[currentIndex - 1];
                RuntimeStorage.CurrentProject.Slides[currentIndex - 1] = mCoreSlide;
                RuntimeStorage.CurrentProject.Slides[currentIndex] = swap;
            }

            EventsManagement.Instance.OnReflectProjectInvoked(this, EventArgs.Empty);
        }

        void miMoveDown_Click(object sender, RoutedEventArgs e)
        {
            int currentIndex = RuntimeStorage.CurrentProject.Slides.IndexOf(mCoreSlide);
            if (currentIndex + 1 < RuntimeStorage.CurrentProject.Slides.Count)
            {
                Slide swap = RuntimeStorage.CurrentProject.Slides[currentIndex + 1];
                RuntimeStorage.CurrentProject.Slides[currentIndex + 1] = mCoreSlide;
                RuntimeStorage.CurrentProject.Slides[currentIndex] = swap;
            }

            EventsManagement.Instance.OnReflectProjectInvoked(this, EventArgs.Empty);
        }

        void miDeleteSlide_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.DeleteSlide(mCoreSlide);
            EventsManagement.Instance.OnReflectProjectInvoked(this, EventArgs.Empty);
        }

        void Instance_ReflectSlideViewerRequested(object sender, EventArgs e)
        {
            ReflectSlide();
        }

        void SlideThumbnail_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            IsSelected = true;
        }

        void SlideThumbnail_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            EventsManagement.Instance.OnSelectedSlideIndexChanged(this, new iTeachSoft.Presentation.Code.Utility.SingleItemEventArgs<int>(RuntimeStorage.CurrentProject.Slides.IndexOf(CoreSlide)));
        }

        private void ReflectSlide()
        {
            System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(FileManager.GetSlidePreviewPath(CoreSlide));
            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(bitmap);
            bitmap.Dispose();

            System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    newBitmap.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            newBitmap.Dispose();

            ImageSource = GraphicsUtility.BitmapSourceToBitmapImage(bitmapSource);
            SlideText = "Slide " + RuntimeStorage.CurrentProject.Slides.IndexOf(CoreSlide).ToString();
            GC.Collect();
        }
    }
}
